from typing import Optional
from unfold.admin import ModelAdmin as UnfoldModelAdmin

class ModelAdmin(UnfoldModelAdmin):
    permission_required: Optional[list[str]] = None
    
    def has_view_permission(self, request, obj=None):
        if request.user.is_superuser:
            return True
        
        if self.permission_required is None:
            return False
        return request.user.has_perms(self.permission_required)

    def has_add_permission(self, request):
        if request.user.is_superuser:
            return True
        
        if self.permission_required is None:
            return False
        return request.user.has_perms(self.permission_required)

    def has_change_permission(self, request, obj=None):
        if request.user.is_superuser:
            return True
        
        if self.permission_required is None:
            return False
        return request.user.has_perms(self.permission_required)

    def has_delete_permission(self, request, obj=None):
        if request.user.is_superuser:
            return True

        if self.permission_required is None:
            return False
        return request.user.has_perms(self.permission_required)

    def has_module_permission(self, request):
        if request.user.is_superuser:
            return True
        
        if self.permission_required is None:
            return False
        return request.user.has_perms(self.permission_required)


class ImportExportFieldUseVerboseNameMixin:
    @classmethod
    def field_from_django_field(cls, field_name, django_field, readonly):
        field = super().field_from_django_field(field_name, django_field, readonly)
        field.column_name = django_field.verbose_name
        return field

